<template>
  <div
    class="fix-type-wrapper"
    :class="{ show: visible }"
  >
    <ul class="type-wrapper">
      <li
        class="cursor"
        @click="all()"
      >
        全部
      </li>
      <li
        v-for="(item, index) in typeList"
        :key="index"
        @click="changeType(item.id)"
      >
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { typeStore } from '@/store/type'
import { useRouter } from 'vue-router'

const store = typeStore()

const router = useRouter()

const visible:any = ref(false)

const typeList:any = computed(() => {
  return store.typeList
})

onMounted(() => {
  window.addEventListener('scroll', headershow)
})
onUnmounted(() => {
  window.removeEventListener('scroll', headershow)
})

const headershow = () => {
  // 头部高度为70px
  const height = 70
  const offsetTop = window.pageYOffset || document.documentElement.scrollTop
  visible.value = offsetTop > height
}
const changeType = (typeId:any) => {
  store.changeTypeId(typeId)
  if (router.currentRoute.value.path !== '/article') {
    router.push('/article')
  }
}
const all = () => {
  if (store.typeId) {
    store.changeTypeId('')
  }
  if (router.currentRoute.value.path !== '/article') {
    router.push('/article')
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/css/constant.scss';

/* 响应式布局,大于540px */
@media screen and (min-width: 540px) {
  .show {
    transform: translateY(-70px);
  }
}
.dark {
  .fix-type-wrapper {
    background: $theme-color3;
  }
}
.fix-type-wrapper {
  position: fixed;
  top: 70px;
  left: 0;
  z-index: 99;
  width: 100%;
  border-bottom: 1px solid $theme-color4;
  background-color: $theme-color6;
  .type-wrapper {
    margin: 0 auto;
    padding: 0;
    height: 40px;
    color: $theme-color1;
    line-height: 40px;
    li {
      display: inline;
      margin-right: 8px;
      cursor: pointer;
    }
  }
}
.show {
  transition: all 0.5s;
  transform: translateY(-70px);
}
</style>
